package com.sesame;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.sesame.entity.SysUserPo;
import com.sesame.entity.UserVo;
import com.sesame.mapping.UserMapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 测试代码复制的性能
 */
public class Test {
    static UserMapper userMapper = UserMapper.INSTANCES;
    public static void main(String[] args) {
        int maxCount = 100000;
        List<SysUserPo> list = getUserList(maxCount);

        System.out.println("转换数据条数:"+maxCount);
        System.out.println();
        convert(list);
        System.out.println();
        convert(list);
        System.out.println();
        convert(list);

    }

    private static void convert(List<SysUserPo> list){
        TimeInterval timer = DateUtil.timer();

        list.stream().map(po->{
            UserVo vo = new UserVo();
            org.springframework.beans.BeanUtils.copyProperties(po, vo);
            return vo;
        }).collect(Collectors.toList());
        System.out.println("org.springframework.beans.BeanUtils.copyProperties 耗时 : "+timer.intervalRestart());

        list.stream().map(po->{
            UserVo vo = new UserVo();
            cn.hutool.core.bean.BeanUtil.copyProperties(po, vo);
            return vo;
        }).collect(Collectors.toList());
        System.out.println("cn.hutool.core.bean.BeanUtil 耗时 : "+timer.intervalRestart());

        list.stream().map(userMapper::toUserVo).collect(Collectors.toList());
        System.out.println("Mappers 耗时 : "+timer.intervalRestart());
    }


    private static List<SysUserPo> getUserList(int num) {
        List<SysUserPo> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(new SysUserPo(
                    "userId" + i,
                    "username" + i,
                    "avatar" + i,
                    1,
                    "1321232323" + i,
                    "email" + i,
                    new Date()
            ));
        }
        return list;
    }
}
